package com.holyn.guofang.viewcommon.crop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.R;
import com.luck.picture.lib.dialog.PictureDialog;
import com.steelkiwi.cropiwa.CropIwaView;
import com.steelkiwi.cropiwa.image.CropIwaResultReceiver;
import com.yarolegovich.mp.MaterialPreferenceScreen;

public class CropActivity extends AppCompatActivity implements CropIwaResultReceiver.Listener{

    private static final String EXTRA_URI = "https://pp.vk.me/c637119/v637119751/248d1/6dd4IPXWwzI.jpg";

    public final static String EXTRA_RESULT_CROP_PATH = "extra_result_crop_path";

    public static Intent callingIntent(Context context, Uri imageUri) {
        Intent intent = new Intent(context, CropActivity.class);
        intent.putExtra(EXTRA_URI, imageUri);
        return intent;
    }

    private CropIwaView cropView;
    private CropViewConfigurator configurator;

    protected PictureDialog dialog;

    private CropIwaResultReceiver cropResultReceiver;


    @Override
    @SuppressWarnings("ConstantConditions")
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("图片剪切");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Uri imageUri = getIntent().getParcelableExtra(EXTRA_URI);
        cropView = (CropIwaView) findViewById(R.id.crop_view);
        cropView.setImageUri(imageUri);

        MaterialPreferenceScreen cropPrefScreen = (MaterialPreferenceScreen) findViewById(R.id.crop_preference_screen);
        configurator = new CropViewConfigurator(cropView, cropPrefScreen);
        cropPrefScreen.setStorageModule(configurator);

        cropResultReceiver = new CropIwaResultReceiver();
        cropResultReceiver.setListener(this);
        cropResultReceiver.register(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_crop, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.done) {
            showLoadinDialog();
            cropView.crop(configurator.getSelectedSaveConfig());
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCropSuccess(Uri croppedUri) {
        LogUtils.d("onCropSuccess : "+croppedUri);
        String path = croppedUri.getPath();
        Intent intent = getIntent();
        intent.putExtra(EXTRA_RESULT_CROP_PATH, path);
        setResult(Activity.RESULT_OK, intent);
        closeLoadingDialog();
        finish();
    }

    @Override
    public void onCropFailed(Throwable e) {
        closeLoadingDialog();
        Toast.makeText(this, "剪切失败，请重新尝试....", Toast.LENGTH_SHORT).show();
    }

    private void showLoadinDialog(){
        if (dialog == null) {dialog = new PictureDialog(CropActivity.this);}
        dialog.show();
    }

    private void closeLoadingDialog(){
        if (dialog != null){
            if (dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cropResultReceiver.unregister(this);
    }
}
